var Mongoose = require('mongoose'),
    util = require('../helpers/util'),
    users = require('../models/user');
var versionSchema = Mongoose.Schema({
    name: {
        type: String,
        default: ''
    },
    male: {
        type: Number,
        default: 0
    },
    female: {
        type: Number,
        default: 0
    },
    description: {
        type: String,
        default: ''
    },
    modules: {
        type: [String],
        default: ['recent', 'leaderboard']
    },
    isLocked: {
        type: Boolean,
        default: false
    }
}, {timestamps: true});

var version = Mongoose.model('Version', versionSchema);

version.createNewVersion = function (newVersion, callback) {
    newVersion.save(callback);
};

version.getAllVersion = function (allVersion, callback) {
    version.aggregate([
        {
            $project: {
                "_id": "$_id",
                "name": "$name",
                "description": "$description",
                "male": "$male",
                "female": "$female",
                "createdAt": "$createdAt",
                "updatedAt": "$updatedAt",
                "modules": "$modules",
                "isLocked": "$isLocked"
            }
        }, {"$sort": {"name": 1}}

    ], callback);
};

version.getVersioning = function (allVersion, callback) {
    version.aggregate([
        {
            $project: {
                "name": "$name",
                "description": "$description",
                "male": "$male",
                "female": "$female"
            }
        }, {"$sort": {"name": 1}}
    ], callback);
};

version.changeLockStatus = function (versionName, callback) {
    version.find({name: versionName}).exec(function (err, version) {
        if (!err) {
            if (version[0].isLocked == false) {
                version[0].isLocked = true;
                version[0].save(callback);
            } else {
                version[0].isLocked = false;
                version[0].save(callback);
            }
        }
    });
};

version.getLockedVersion = function (callback) {
    version.find({isLocked: true}, {name: 1, _id: 0}, callback);
};

version.assignRandomVersion = function (callback) {
    version.aggregate([
        {
            "$match": {isLocked: false}
        },
        {
            "$sample": {size: 1}
        }], callback)
};

version.updateVersionCount = function (newVersion, newGender, oldVersion, oldGender) {
    if (newVersion != '') {
        version.find({name: newVersion}).exec(function (err, versions) {
            if (!err) {
                if (newGender == "Male") {
                    versions[0].male = versions[0].male + 1;
                } else {
                    versions[0].female = versions[0].female + 1;
                }
                versions[0].save();
            }
        });
    }
    if (oldVersion != '' ){
        version.find({name: oldVersion}).exec(function (err, versions) {
            if (!err) {
                if (oldGender == "Male") {
                    versions[0].male = versions[0].male - 1;
                } else {
                    versions[0].female = versions[0].female - 1;
                }
                versions[0].save();
            }
        });
    }
};

module.exports = version;